package com.njworkorder.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njworkorder.Entity.TasksNum;
import com.njworkorder.Service.TasksNumService;
import com.njworkorder.Mapper.TasksNumMapper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
* @author iron
* @description 针对表【tasks_num】的数据库操作Service实现
* @createDate 2025-06-20 14:04:41
*/
@Service
public class TasksNumServiceImpl extends ServiceImpl<TasksNumMapper, TasksNum>
    implements TasksNumService{

    @Async
    @Override
    public void updateTasksNum(int num) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate today = LocalDate.now();
        String sTime = today.format(formatter);

        TasksNum  tasksNum = new TasksNum();
        tasksNum.setNum(num);
        tasksNum.setCreateTime(new Date());

        LambdaQueryWrapper<TasksNum> queryWrapper = new LambdaQueryWrapper<TasksNum>()
                .eq(TasksNum::getCreateTime, sTime);
        TasksNum tasksNum1 = baseMapper.selectOne(queryWrapper);
        if(tasksNum1 == null){
            baseMapper.insert(tasksNum);
            return;
        }

        LambdaUpdateWrapper<TasksNum> updateWrapper = new LambdaUpdateWrapper<TasksNum>()
                .set(TasksNum::getNum,num)
                .eq(TasksNum::getId,tasksNum1.getId());
        baseMapper.update(updateWrapper);
    }
}




